/**
 * @Author 赖治宇
 * @CreatedAt 2023/12/10 11:26
 * @UpdatedAt 2023/12/11 10:37
 * @Deascription 异步编程案例
 */
const { fetchGeneratorData } = require('./api')

// 生成器函数
function* addGenerator(a, b) {
    const res1 = yield fetchGeneratorData(a + b);
    const res2 = yield fetchGeneratorData(res1 * 2);
    const res = yield fetchGeneratorData(res2 * 3);
    return res;
}

// 初始化执行，g为一个生成器对象，拥有next() return()等方法
const g = addGenerator(1, 2)

// 跳过暂停进入下一步，result为固定的数据结构{value:any,done:boolean}
const result = g.next()
console.log('打印执行结果0', result)

result.value.then(res => {
    console.log('打印执行结果1', res)
    const res1 = g.next(res)
    res1.value.then(res => {
        console.log('打印执行结果2', res)
        const res2 = g.next(res)
        res2.value.then(res => {
            console.log('打印执行结果3', res)
        })
    })
})